package com.tititi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tititi.entity.Device;
import com.tititi.entity.MyPage;
import com.tititi.entity.Result;
import com.tititi.service.IDeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 设备
 * @author tang
 */
@RestController
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;

    @RequestMapping("/upgrade/info")
    public Result upgrade(Device device){
        if(StringUtils.isBlank(device.getDeviceId())){
            return Result.error("device id can't be null!");
        }
        deviceService.save(device);
        return Result.ok();
    }




    @RequestMapping("/list")
    public Result list(String deviceName,String deviceId,Long current,Long size){
        if(current==null || size==null){
            return Result.error("current or size can't be null");
        }
        IPage<Device> page=new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        QueryWrapper<Device> queryWrapper=new QueryWrapper<>();
        queryWrapper
                .eq(!StringUtils.isBlank(deviceName),"device_name",deviceName)
                .eq(!StringUtils.isBlank(deviceId),"device_id",deviceId)
                .orderByDesc("create_time");
        page = deviceService.page(page, queryWrapper);
        return Result.ok(new MyPage<>(page.getRecords(), page.getTotal()));
    }

}
